package tau.pimpmyfreedb.GUI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import tau.pimpmyfreedb.common.types.EditData;
import tau.pimpmyfreedb.common.types.MusicItem;
import tau.pimpmyfreedb.common.types.Tuple;
import tau.pimpmyfreedb.dal.DataType;

public class SWTUtil {

    private static Display display = new Display();

    public static Display getDisplay() {
	return display;
    }

    public static Shell getShell() {
	Shell shell = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.MIN);
	return shell;
    }

    public static Shell getShell(Shell parent) {
	Shell shell = new Shell(parent, SWT.CLOSE | SWT.TITLE | SWT.MIN
		| SWT.APPLICATION_MODAL);
	return shell;
    }
    
    public static Shell getResizeableShell() {
	Shell shell = new Shell(display, SWT.SHELL_TRIM);
	return shell;
    }

    public static Shell getDialog() {
	Shell shell = new Shell(display, SWT.APPLICATION_MODAL
		| SWT.DIALOG_TRIM);
	return shell;
    }

    public static Shell getDialog(Shell parent) {
	Shell shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
	return shell;
    }

    public static void readAndDispatch(Predicate predicate) {
	while (predicate.isTrue()) {
	    if (!display.readAndDispatch()) {
		display.sleep();
	    }
	}
    }

    public static void dispose() {
	display.dispose();
    }

    public static void errorBox(final String msg) {
	display.asyncExec(new Runnable() {
	    @Override
	    public void run() {
		Shell sh = getShell();
		MessageBox msgBox = new MessageBox(sh, SWT.ICON_ERROR);
		msgBox.setText("Application Error");
		final String msg2 = msg == null ? "Unknown Error" : msg;
		msgBox.setMessage(msg2);
		msgBox.open();
		sh.dispose();
	    }
	});
    }

    public static void errorBox(Exception e) {
	String msg = e.getMessage();
	if (msg == null)
	    msg = e.toString();
	errorBox(msg);
    }

    public static void messageBox(final String msg) {
	if (msg == null)
	    return;

	display.asyncExec(new Runnable() {
	    @Override
	    public void run() {
		Shell shell = getShell();
		MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION);
		msgBox.setMessage(msg);
		msgBox.open();
		shell.dispose();
	    }
	});
    }

    public static void PopulateList(String[] string, List list) {
	for (int i = 0; i <= string.length - 1; i++) {
	    list.add(string[i]);
	}
    }

    public static void PopulateList(MusicItem[] string, List list) {
	for (int i = 0; i <= string.length - 1; i++) {
	    list.add(string[i].getName());
	}
    }

    public static String dataType2String(DataType dataType) {
	switch (dataType) {
	case ALBUM:
	    return "Album";
	case ARTIST:
	    return "Artist";
	case TRACK:
	    return "Track";
	default:
	    return "";
	}
    }

    public static void printEditData(EditData editData) {
	System.out.println("*****************\nCreating:");
	for (MusicItem musicItem : editData.getItemsToCreate()) {
	    System.out.println(musicItem.getName() + " of type: "
		    + dataType2String(musicItem.getDataType()));
	}

	System.out.println("*****************\nAdding Relations:");

	for (Tuple<MusicItem, MusicItem> tuple : editData.getRelations()) {
	    System.out.println(tuple.getX().getName() + " of type: "
		    + dataType2String(tuple.getX().getDataType())
		    + "     TO    " + tuple.getY().getName() + " of type: "
		    + dataType2String(tuple.getY().getDataType()));
	}

    }

    public static int String2Int(String string) {
	if (string.isEmpty()){
	    return 0;
	}
	return Integer.parseInt(string);
	    
    }

}
